<?php

// +----------------------------------------------------------------------
// | Apprh.Shop
// +----------------------------------------------------------------------
// | Copyright (c) 2017-2027 http://www.apprh.com All rights reserved.
// +----------------------------------------------------------------------
// | Notice: This code is not open source, it is strictly prohibited
// |         to distribute the copy, otherwise it will pursue its
// |         legal responsibility.
// +----------------------------------------------------------------------
// | Author: pp <124910168@qq.com>
// +----------------------------------------------------------------------

namespace app\home\controller;

use controller\BasicHome;
use think\Db;

/**
 * 搜索
 * Class Search
 * @package app\home\controller
 * @author pp <124910168@qq.com>
 * @date 2020/12/15 10:41
 */
class Search extends BasicHome
{
    public function _initialize()
    {
        parent::_initialize();
        $this->pageSize = sysconf('page_default_num', 20);
    }
    
    //首页
    public function pre(){
        return $this->fetch();
    }
    
    //列表
    public function index(){
        $get = $this->request->get();
        // halt($get);
        switch($get['name']){
            case 'all':
                $data = $this->all();
                break;
            case 'characteristic':
                $data = $this->characteristic();
                break;
            case 'client':
                $data = $this->client();
                break;
            case 'clinic':
                $data = $this->clinic();
                break;
            case 'demonstration':
                $data = $this->demonstration();
                break;
            case 'drug':
                $data = $this->drug();
                break;
            case 'prescription':
                $data = $this->prescription();
                break;
            case 'news':
                $data = $this->news();
                break;
            case 'courseware':
                $data = $this->courseware();
                break;
            default:
                $data = $this->all();
                break;
        }
        // halt($data);
        $data['kw'] = !empty($get['kw']) ? $get['kw'] : '';
        $data['m'] = ($get['name'] != 'all') ? $get['name'] : '';
        return $this->fetch('', $data);
    }

    //全站
    public function all(){
        $get = $this->request->get();
        $modules = ['article', 'audio', 'characteristic', 'client', 'clinic', 'demonstration', 'drug', 'picture', 'prescription', 'video'];
        $arrs = $ass = [];
        foreach ($modules as $k => $v) {
            $where = ['status' => 1];
            if(in_array($v, ['article', 'audio', 'picture', 'video'])){
                $where['catid'] = ['in', [2,3,4,5,6]];//默认 图文 理论知识
                $where['title|keywords|description|content'] = ['like', "%".$get['kw']."%"];
            }elseif($v == 'client'){
                $where['title|username|truename|inheritor|unit_name|desc|honor|advantage'] = ['like', "%".$get['kw']."%"];
            }else{
                $where['title|desc|content'] = ['like', "%".$get['kw']."%"];
            }
            // halt($where);
            $list = Db::name($v)->where($where)->select();
            if($list){
                foreach ($list as $kk => $vv) {
                    $arrs[$k][$kk] = $vv;
                    if($vv['catid'] && in_array($vv['catid'], [2,3,4,5,6])){
                        if($vv['catid'] == 6){
                            $m = 'news';
                        }else{
                            $m = 'courseware';
                        }
                    }else{
                        $m = $v;
                    }
                    $arrs[$k][$kk]['m'] = $m;
                
                    if(empty($ass)){
                        $ass[$kk] = $arrs[$k][$kk];
                    }else{
                        array_push($ass, $arrs[$k][$kk]);
                    }
                }
            }
        }
        // $ass = a2p($ass);
        $ass = $this->mypage($ass, $this->pageSize);
        // halt($ass);

        $data = [
            'list' => $ass,
            'page' => $ass->render(),
            'snum' => count($ass),
        ];
        return $data;
    }

    //诊疗
    public function characteristic(){
        $get = $this->request->get();
        
        $map = ['status' => 1];

        // 应用搜索条件
        foreach (['kw', 'aid', 'nid', 'did'] as $key) {
            if (isset($get[$key]) && $get[$key] !== '') {
                if(in_array($key, ['aid', 'nid', 'did'])){
                    $map[$key] = [
                        ['eq', $get[$key]],
                        ['like', $get[$key].",%"],
                        ['like', "%,".$get[$key].",%"],
                        ['like', "%,".$get[$key]],
                        'or',
                    ];
                }else{
                    $map['title|desc|content'] = ['like', "%".$get[$key]."%"];
                }
            }
        }

        $list = $this->characteristic->getPageLists($map, 'createtime desc', '', $this->pageSize);
        // halt($list);
        if($list[0]){
            foreach ($list[0] as $k => $v) {
                $list[0][$k]['m'] = 'characteristic';
            }
        }
        // halt($list[0]);
        $data = [
            'list' => $list[0],
            'page' => $list[1],
            'snum' => count($list[0]),
        ];
        return $data;
    }

    //传承人
    public function client(){
        $get = $this->request->get();
        
        $map = ['status' => 1];

        // 应用搜索条件
        foreach (['kw', 'aid', 'nid', 'did'] as $key) {
            if (isset($get[$key]) && $get[$key] !== '') {
                if(in_array($key, ['aid', 'nid', 'did'])){
                    $map[$key] = [
                        ['eq', $get[$key]],
                        ['like', $get[$key].",%"],
                        ['like', "%,".$get[$key].",%"],
                        ['like', "%,".$get[$key]],
                        'or',
                    ];
                }else{
                    $map['username|truename|honor|advantage|phone|id_no|cert_no|desc|content'] = ['like', "%".$get[$key]."%"];
                }
            }
        }

        $list = $this->client->getPageLists($map, 'create_at desc', '', $this->pageSize);
        if($list[0]){
            foreach ($list[0] as $k => $v) {
                $list[0][$k]['honors'] = $list[0][$k]['honor'] ? explode("\r\n", $v['honor']) : [];
                $list[0][$k]['advantages'] = $list[0][$k]['advantage'] ? explode("\r\n", $v['advantage']) : [];
                $list[0][$k]['m'] = 'client';
            }
        }
        // halt($list);

        $data = [
            'list' => $list[0],
            'page' => $list[1],
            'snum' => count($list[0]),
        ];
        return $data;
    }

    //临床病案
    public function clinic(){
        $get = $this->request->get();
        
        $map = ['status' => 1];

        // 应用搜索条件
        foreach (['kw', 'aid', 'cid', 'pid'] as $key) {
            if (isset($get[$key]) && $get[$key] !== '') {
                if(in_array($key, ['aid', 'cid', 'pid'])){
                    $map[$key] = [
                        ['eq', $get[$key]],
                        ['like', $get[$key].",%"],
                        ['like', "%,".$get[$key].",%"],
                        ['like', "%,".$get[$key]],
                        'or',
                    ];
                }else{
                    $map['title|desc|content|illness|history|diagnosis|process|result'] = ['like', "%".$get[$key]."%"];
                }
            }
        }

        $list = $this->clinic->getPageLists($map, 'createtime desc', '', $this->pageSize);
        // halt($list);
        if($list[0]){
            foreach ($list[0] as $k => $v) {
                $list[0][$k]['m'] = 'clinic';
            }
        }

        $data = [
            'list' => $list[0],
            'page' => $list[1],
            'snum' => count($list[0]),
        ];
        return $data;
    }

    //推广
    public function demonstration(){
        $get = $this->request->get();
        
        $map = ['status' => 1];

        // 应用搜索条件
        foreach (['kw', 'aid', 'nid', 'did'] as $key) {
            if (isset($get[$key]) && $get[$key] !== '') {
                if(in_array($key, ['aid', 'nid', 'did'])){
                    $map[$key] = ['eq', $get[$key]];
                }else{
                    $map['title|desc|content'] = ['like', "%".$get[$key]."%"];
                }
            }
        }

        $list = $this->demonstration->getPageLists($map, 'createtime desc', '', $this->pageSize);
        // halt($list);
        if($list[0]){
            foreach ($list[0] as $k => $v) {
                $list[0][$k]['m'] = 'demonstration';
            }
        }

        $data = [
            'list' => $list[0],
            'page' => $list[1],
            'snum' => count($list[0]),
        ];
        return $data;
    }

    //药品
    public function drug(){
        $get = $this->request->get();
        
        $map = ['status' => 1];

        // 应用搜索条件
        foreach (['kw', 'aid', 'nid', 'did', 'cid'] as $key) {
            if (isset($get[$key]) && $get[$key] !== '') {
                if(in_array($key, ['aid', 'nid', 'did', 'cid'])){
                    $map[$key] = [
                        ['eq', $get[$key]],
                        ['like', $get[$key].",%"],
                        ['like', "%,".$get[$key].",%"],
                        ['like', "%,".$get[$key]],
                        'or',
                    ];
                }else{
                    $map['title|alias|desc|content|character|efficacy'] = ['like', "%".$get[$key]."%"];
                }
            }
        }

        $list = $this->drug->getPageLists($map, 'createtime desc', '', $this->pageSize);
        // halt($list);
        if($list[0]){
            foreach ($list[0] as $k => $v) {
                $list[0][$k]['m'] = 'drug';
            }
        }

        $data = [
            'list' => $list[0],
            'page' => $list[1],
            'snum' => count($list[0]),
        ];
        return $data;
    }

    //单方
    public function prescription(){
        $get = $this->request->get();
        
        $map = ['status' => 1];

        // 应用搜索条件
        foreach (['kw', 'aid', 'nid', 'did'] as $key) {
            if (isset($get[$key]) && $get[$key] !== '') {
                if(in_array($key, ['aid', 'nid', 'did'])){
                    $map[$key] = [
                        ['eq', $get[$key]],
                        ['like', $get[$key].",%"],
                        ['like', "%,".$get[$key].",%"],
                        ['like', "%,".$get[$key]],
                        'or',
                    ];
                }else{
                    $map['title|desc|content|usage'] = ['like', "%".$get[$key]."%"];
                }
            }
        }

        $list = $this->prescription->getPageLists($map, 'createtime desc', '', $this->pageSize);
        // halt($list);
        if($list[0]){
            foreach ($list[0] as $k => $v) {
                $list[0][$k]['m'] = 'prescription';
            }
        }

        $data = [
            'list' => $list[0],
            'page' => $list[1],
            'snum' => count($list[0]),
        ];
        return $data;
    }

    //新闻
    public function news(){
        $get = $this->request->get();
        
        $map = ['status' => 1, 'catid' => 6];

        // 应用搜索条件
        foreach (['kw', 'aid', 'userid', 'catid'] as $key) {
            if (isset($get[$key]) && $get[$key] !== '') {
                if(in_array($key, ['aid', 'userid', 'catid'])){
                    $map[$key] = [
                        ['eq', $get[$key]],
                        ['like', $get[$key].",%"],
                        ['like', "%,".$get[$key].",%"],
                        ['like', "%,".$get[$key]],
                        'or',
                    ];
                }else{
                    $map['title|description|content'] = ['like', "%".$get[$key]."%"];
                }
            }
        }

        $list = $this->article->getPageLists($map, 'createtime desc', '', $this->pageSize);
        // halt($list);
        if($list[0]){
            foreach ($list[0] as $k => $v) {
                $list[0][$k]['m'] = 'news';
            }
        }

        $data = [
            'list' => $list[0],
            'page' => $list[1],
            'snum' => count($list[0]),
        ];
        return $data;
    }

    //课件
    public function courseware(){
        $get = $this->request->get();
        $catid = input('cid', 2);//默认 图文 理论知识
        // halt($catid);
        
        $map = ['status' => 1, 'catid' => $catid];
        $m = $this->category->getValue(['id' => $catid], 'module');

        // 应用搜索条件
        foreach (['kw', 'aid', 'userid'] as $key) {
            if (isset($get[$key]) && $get[$key] !== '') {
                if(in_array($key, ['aid', 'userid'])){
                    $map[$key] = [
                        ['eq', $get[$key]],
                        ['like', $get[$key].",%"],
                        ['like', "%,".$get[$key].",%"],
                        ['like', "%,".$get[$key]],
                        'or',
                    ];
                }else{
                    $uids = Db::name('Client')->where('username', 'like', "%{$get[$key]}%")->column('id');
                    if($uids){
                        $map['userid'] = ['in', $uids];
                    }else{
                        $map['title|username|keywords|description|content'] = ['like', "%".$get[$key]."%"];
                    }
                }
            }
        }

        $list = Db::name($m)
        ->where($map)
        ->order('sort asc,createtime desc')
        ->paginate($this->pageSize);
        // halt($list);
        
        // 获取分页显示
        $page = $list->render();
        $list = $list->toArray();
        // halt($list['data']);
        if($list['data']){
            foreach ($list['data'] as $k => $v) {
                $list['data'][$k]['m'] = 'courseware';
            }
        }

        $data = [
            'list' => $list['data'],
            'page' => $page,
            'snum' => count($list['data']),
        ];
        return $data;
    }

}
